package com.apes.scm.pur.redemption.service;

import com.apes.framework.api.DomainService;
import com.apes.scm.masterdata.product.service.ProductUomService;
import com.apes.scm.pur.redemption.model.Redemption;
import com.apes.scm.pur.redemption.repository.RedemptionRepository;
import com.apes.scm.voucher.model.Voucher;
import com.apes.scm.voucher.model.VoucherItem;
import com.apes.scm.voucher.service.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

@Service("redemptionVoucherService")
public class RedemptionVoucherService extends DomainService {
    @Autowired
    private RedemptionRepository redemptionRepository;
    @Autowired
    private ProductUomService uomService;

    public Voucher flowRedemptionVoucher(Redemption redemption) {
        Voucher voucher = new Voucher();
        voucher.setCompany(redemption.getCompany());
        voucher.setDept(redemption.getDept());
        voucher.setClerk(redemption.getClerk());
        voucher.setContract(redemption.getContract());
        voucher.setPartner(redemption.getSupplier());
        voucher.setInventoryMode(redemption.getCooperation().getInventoryMode());
        voucher.setAccountingMethod(redemption.getCooperation().getAccountingMethod());
//        voucher.setScene(redemption.getScene());
        voucher.setBusinessType(BusinessType.PURCHASE_RETURN);
        voucher.setOrigin(redemption.getId());
        voucher.setOriginBusinessType(redemption.getInnerBusinessType());
        voucher.setSource(redemption.getId());
        voucher.setSourceBusinessType(redemption.getInnerBusinessType());
        voucher.setReturned(true); // 退回标记

        // 从表信息
        Set<VoucherItem> voucherItems = new HashSet();
        getRedemptionVoucherItems(redemption, voucherItems);
        voucher.setVoucherItems(voucherItems);

        return voucher;
    }

    /**
     * 拼接凭证明细
     * @param redemption
     * @param voucherItems
     */
    public void getRedemptionVoucherItems(Redemption redemption, Set<VoucherItem> voucherItems) {
        redemption.getRedemptionItems().forEach(redemptionItem -> {
            VoucherItem voucherItem = new VoucherItem();
            voucherItem.setProduct(redemptionItem.getProduct());
            voucherItem.setUom(redemptionItem.getUom());
            voucherItem.setTax(redemptionItem.getTax());
            voucherItem.setOwner(redemption.getLocation().getCompany());
            voucherItem.setProductQty(redemptionItem.computeQtyUnit(redemptionItem.getQty()));
            voucherItem.setPriceUnit(redemptionItem.computePriceUnit());
            voucherItem.setSourceItemId(String.valueOf(redemptionItem.getId()));
            voucherItem.setInventoryBatch(redemptionItem.getInventoryBatch());
            voucherItem.setOriginItemId(redemptionItem.getOriginItemId());
            voucherItem.setSourceItemId(String.valueOf(redemptionItem.getId()));
            voucherItems.add(voucherItem);
        });
    }
}
